package com.baidu.health.service;

import com.baidu.health.mapper.TbGoodsMapper;
import com.baidu.health.pojo.TbGoods;
import com.baidu.health.util.ResultResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baidu.health.pojo.TbStore;
import com.baidu.health.mapper.TbStoreMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 *
 */
@Service
public class TbStoreServiceImpl extends ServiceImpl<TbStoreMapper, TbStore>
    implements TbStoreService{
    @Resource
    private TbStoreMapper mapper;
    @Resource
    private TbGoodsMapper tbGoodsMapper;



    @Override
    public ResultResponse storeadd(TbStore tbStore) {
        mapper.insert(tbStore);
        return ResultResponse.SUCCESS("添加成功");
    }

    @Override
    public ResultResponse storedel(String storeId) {
        mapper.deleteById(storeId);
        return ResultResponse.SUCCESS("删除成功");
    }

    @Override
    public ResultResponse storeupdate(TbStore tbStore) {
        mapper.updateById(tbStore);
        return ResultResponse.SUCCESS("修改成功");
    }

    @Override
    public ResultResponse storelist(Integer page, Integer size) {
        Page<TbStore> tbStorePage = new Page<>(page, size);
        Page<TbStore> list1= mapper.storelist(tbStorePage);
        return ResultResponse.SUCCESS(list1);
    }

    @Override
    public ResultResponse rolelist(Integer page, Integer size, Integer userId, Byte userRole) {
        if (userRole==1){
            Page<TbStore> tbStorePage = new Page<>(page, size);
            Page<TbStore> list1= mapper.rolelist(tbStorePage,userId);
            return ResultResponse.SUCCESS(list1);
        }else{
            Page<TbStore> page1 = new Page<>(page, size);
            Page<TbStore> list1 = mapper.rolelist(page1, null);
            return ResultResponse.SUCCESS(list1);
        }
    }

    @Override
    public ResultResponse storegoodslist(Integer page, Integer size,Integer storeId) {

            Page<TbGoods> tbGoodsPage = new Page<>(page, size);
            Page<TbGoods> list1=  mapper.storegoodslist(tbGoodsPage,storeId);
            return ResultResponse.SUCCESS(list1);

    }

    @Override
    public ResultResponse storegoodsadd(TbGoods tbGoods, Integer storeId) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());
        tbGoods.setGoodsUpdatetime(format);
        tbGoods.setGoodsState(0);
        tbGoods.setStoreId(storeId);
        tbGoodsMapper.insert(tbGoods);
        return ResultResponse.SUCCESS(storeId);
    }

    @Override
    public ResultResponse auditgoods(Integer goodsId, Integer goodsState) {
        TbGoods goods = tbGoodsMapper.selectById(goodsId);
        goods.setGoodsState(goodsState);
        goods.setUpdateTime(new Date());
        tbGoodsMapper.updateById(goods);
        return ResultResponse.SUCCESS("修改状态成功");
    }

    @Override
    public ResultResponse ckhecjlist(Map<String, Object> params) {
        QueryWrapper<TbGoods> tbGoodsQueryWrapper = new QueryWrapper<>();
        tbGoodsQueryWrapper.lambda().eq(TbGoods::getGoodsState,1);
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse adminlist(Integer page, Integer size, Integer userId, Byte userRole, Integer storeId) {
        if (userRole==1){
                Page<TbGoods> tbGoodsPage = new Page<>(page, size);
                Page<TbGoods> list1= mapper.adminlist(tbGoodsPage,userId,storeId);
                return ResultResponse.SUCCESS(list1);
        }else {
            Page<TbGoods> tbGoodsPage = new Page<>(page, size);
            Page<TbGoods> list1= mapper.adminlist(tbGoodsPage,null,null);
            return ResultResponse.SUCCESS(list1);
        }
    }

    @Override
    public ResultResponse updategoods(Integer goodsId, TbGoods tbGoods) {
        mapper.updategoods(goodsId,tbGoods);
        return ResultResponse.SUCCESS();
    }


}




